import Vue from 'vue'
import Router from 'vue-router'
import mystore from '../store/index'
import { MessageBox } from 'element-ui'

Vue.use(Router)


const manage = r => require.ensure([], () => r(require('@/page/manage')), 'manage');
const home = r => require.ensure([], () => r(require('@/page/home')), 'home');
const addShop = r => require.ensure([], () => r(require('@/page/addShop')), 'addShop');
const addGoods = r => require.ensure([], () => r(require('@/page/addGoods')), 'addGoods');
const userList = r => require.ensure([], () => r(require('@/page/userList')), 'userList');
const shopList = r => require.ensure([], () => r(require('@/page/shopList')), 'shopList');
const foodList = r => require.ensure([], () => r(require('@/page/foodList')), 'foodList');
const orderList = r => require.ensure([], () => r(require('@/page/orderList')), 'orderList');
const adminList = r => require.ensure([], () => r(require('@/page/adminList')), 'adminList');
const visitor = r => require.ensure([], () => r(require('@/page/visitor')), 'visitor');
const newMember = r => require.ensure([], () => r(require('@/page/newMember')), 'newMember');
const uploadImg = r => require.ensure([], () => r(require('@/page/uploadImg')), 'uploadImg');
const vueEdit = r => require.ensure([], () => r(require('@/page/vueEdit')), 'vueEdit');
const adminSet = r => require.ensure([], () => r(require('@/page/adminSet')), 'adminSet');
const sendMessage = r => require.ensure([], () => r(require('@/page/sendMessage')), 'sendMessage');
const explain = r => require.ensure([], () => r(require('@/page/explain')), 'explain');
const layout = r => require.ensure([], () => r(require('@/page/layout')), 'layout');

const login = r => require.ensure([], () => r(require('@/layout/login')), 'login');
const main = r => require.ensure([], () => r(require('@/layout/main')), 'main');
const index = r => require.ensure([], () => r(require('@/layout/index')), 'index');



const routes = [
  {
    path: '/',
    redirect: '/login'
  },
	{
		path: '/login',
		component: login,
    meta: { // 需要登录才能进入的页面可以增加一个meta属性
      requireAuth: false
    }
	},
  {
    path: '/main',
    component: main,
    children: [{
        path: '',
        component: index,
        meta: { // 需要登录才能进入的页面可以增加一个meta属性
          requireAuth: true
        }
      },
      {
        path: '/addShop',
        component: addShop,
        meta: { // 需要登录才能进入的页面可以增加一个meta属性
          requireAuth: true
        }
      }
    ]
  },
/*============================================*/

	{
		path: '/manage',
		component: manage,
		name: '',
		children: [{
			path: '',
			component: home,
			meta: [],
		},{
			path: '/addShop',
			component: addShop,
			meta: ['添加数据', '添加商铺'],
		},{
			path: '/addGoods',
			component: addGoods,
			meta: ['添加数据', '添加商品'],
		},{
			path: '/userList',
			component: userList,
			meta: ['数据管理', '用户列表'],
		},{
			path: '/shopList',
			component: shopList,
			meta: ['数据管理', '商家列表'],
		},{
			path: '/foodList',
			component: foodList,
			meta: ['数据管理', '食品列表'],
		},{
			path: '/orderList',
			component: orderList,
			meta: ['数据管理', '订单列表'],
		},{
			path: '/adminList',
			component: adminList,
			meta: ['数据管理', '管理员列表'],
		},{
			path: '/visitor',
			component: visitor,
			meta: ['图表', '用户分布'],
		},{
			path: '/newMember',
			component: newMember,
			meta: ['图表', '用户数据'],
		},{
			path: '/uploadImg',
			component: uploadImg,
			meta: ['文本编辑', 'MarkDown'],
		},{
			path: '/vueEdit',
			component: vueEdit,
			meta: ['编辑', '文本编辑'],
		},{
			path: '/adminSet',
			component: adminSet,
			meta: ['设置', '管理员设置'],
		},{
			path: '/sendMessage',
			component: sendMessage,
			meta: ['设置', '发送通知'],
		},{
			path: '/explain',
			component: explain,
			meta: ['说明', '说明'],
		},{
        path: '/layout',
        component: layout,
        meta: [],
      }]
	}
]

const router=new Router({
	routes,
	strict: process.env.NODE_ENV !== 'production',
})

// 判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
  console.log("路由拦截="+to.meta.requireAuth)
  console.log("token="+mystore.state.user.token)
  if (to.meta.requireAuth){  // 判断该路由是否需要登录权限
    if (mystore.state.user.token) {  // 通过vuex state获取当前的token是否存在
      next();
    } else {
      MessageBox.confirm("未登录或登录超时", {
        confirmButtonText: '重新登录',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.clear();// 重新登录清空所有数据
        next({
          path: '/login',
          query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
      })
    }
  } else {
    next();
  }
})

export default router
